package com.cqemme.activitiservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cqemme.activitiservice.dao.AlarmCloseDesDao;
import com.cqemme.activitiservice.entity.AlarmCloseDesEntity;
import com.cqemme.activitiservice.po.CloseTaskPo;
import com.cqemme.activitiservice.po.CompleteTaskPo;
import com.cqemme.activitiservice.service.ActivitiService;
import com.cqemme.activitiservice.service.AlarmService;
import com.cqemme.comm.po.CurrentLoginUser;
import com.cqemme.comm.po.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 报警控制器
 *
 * @author nejery
 */
@RestController
@RequestMapping("/alarm")
@Slf4j
@Api(tags = "报警流程查询")
public class AlarmController {

    @Resource
    private ActivitiService activitiService;


    @Resource
    private AlarmService alarmService;

    @Autowired
    private AlarmCloseDesDao alarmCloseDesDao;

    /**
     * 校徽报警
     *
     * @param student
     */

    @ApiOperation(value = "校徽产生sos报警（node内部调用）", notes = "daibin")
    @RequestMapping(value = "/sosAlarm", method = RequestMethod.POST)
    @ResponseBody
    public Result sosAlarm(@RequestBody String student) {
        student = "{\"IMSI-id\":\"460045734304556\",\"Electricity\":\"4108\",\"distance\":5604,\"orgCode\":500107002,\"Sos-Sign\":\"1\",\"schoolId\":500107002,\"Nb-Strength\":\"23\",\"IMEI-id\":\"869075030145728\",\"Gps\":[{\"Address\":\"重庆市渝中区石油路街道金银湾215附2号楼\",\"Latitude\":\"29.5490140822686\",\"_id\":\"5e13ebad7e8f074658cbec34\",\"Longitude\":\"106.492492133174\"}],\"Student\":{\"studentAge\":7,\"updateDate\":\"2020-01-02 14:01:58\",\"imeiNo\":869075030145728,\"studentTel\":18956895689,\"studentNumber\":20190625,\"studentName\":\"赵思\",\"idCardNo\":\"500225123456789\",\"headImage\":\"https://images.cqemme.com/images/png/default/file662299160141103104.png\",\"dataStatus\":1,\"id\":93,\"studentSex\":1,\"createDate\":\"2020-01-02 14:01:58\"},\"sosSite\":\"in\",\"Step-Count\":\"270\"}";
        return alarmService.sosAlarm(student);
    }


    @ApiOperation(value = "根据自己的手机号查询已办/待办任务/候选人任务", notes = "daibin")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskType", value = "任务状态(1:已办 2:代理人任务 3:候选人任务)", required = true, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageNum", value = "分页页码", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "分页数据条数", paramType = "query", dataType = "Integer"),
    })
    @RequestMapping(value = "/selectTask", method = RequestMethod.GET)
    @ResponseBody
    public Result selectTask(
            CurrentLoginUser currentLoginUser,
            @RequestParam Integer taskType,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {

        Map<String, Object> stringObjectMap = null;
        Result result = new Result();
        if (taskType == 1) {
            stringObjectMap = alarmService.doneTaskList(currentLoginUser, pageNum, pageSize);
        } else if (taskType == 2) {
            stringObjectMap = alarmService.assignTask(currentLoginUser, pageNum, pageSize);
        } else if (taskType == 3) {
            stringObjectMap = alarmService.candidateTask(currentLoginUser, pageNum, pageSize);
        }
        result.setData(stringObjectMap);
        return result;
    }


    @ApiOperation(value = "候选人签收任务", notes = "nejery")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "节点id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "userName", value = "签收者姓名", required = true, paramType = "query", dataType = "String"),
    })
    @RequestMapping(value = "/claimTask", method = RequestMethod.GET)
    @ResponseBody
    public Result claimTask(
            CurrentLoginUser currentLoginUser,
            @RequestParam(value = "taskId", required = true) String taskId,
            @RequestParam(value = "userName", required = true) String userName
    ) {
        Result result = new Result();
        alarmService.claimTask(taskId, currentLoginUser);
        return result;
    }

    /**
     * 根据流程id 查流程详情
     * (查所有的流程变量展示学生的数据-查走过的流程节点展示进行步骤-
     * 根据流程id和人员id查当前任务展示这个人是否是本任务的完成人)
     *
     * @param instanceId 流程实例id
     * @return 结果
     */
    @ApiOperation(value = "根据流程id查流程详情", notes = "daibin")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "instanceId", value = "流程实例id", required = true, paramType = "query", dataType = "String"),
    })
    @RequestMapping(value = "/selectInstanceInfo", method = RequestMethod.GET)
    @ResponseBody
    public Result selectInstanceInfo(
            @RequestParam String instanceId
    ) {
        Result result = new Result();
        Map<String, Object> stringObjectMap = activitiService.selectInstanceInfo(instanceId);
        result.setData(stringObjectMap);
        return result;
    }


    @ApiOperation(value = "查询任务数量", notes = "nejery")
    @RequestMapping(value = "/selectTaskCount", method = RequestMethod.GET)
    @ResponseBody
    public Result selectTaskCount(CurrentLoginUser currentLoginUser) {
        Result result = new Result();
        Map<String, Object> stringObjectMap = alarmService.selectTaskCount(currentLoginUser);
        result.setData(stringObjectMap);
        return result;
    }


    /**
     * 其中有一个保安抵达报警地点
     *
     * @param json json
     * @return 处理结果
     */
    @ApiOperation(value = "保安抵达报警地点（node内部调用）", notes = "daibin")
    @RequestMapping(value = "/securityArrival", method = RequestMethod.POST)
    @ResponseBody
    public Result securityArrival(@RequestBody String json) {
        return new Result();
    }

    /**
     * 成员完成任务
     *
     * @param completeTaskPo 完成任务Po
     * @return 结果
     */
    @ApiOperation(value = "完成任务", notes = "nejery")
    @RequestMapping(value = "/completeTask", method = RequestMethod.POST)
    @ResponseBody
    public Result gearTask(
            CurrentLoginUser currentLoginUser,
            @RequestBody CompleteTaskPo completeTaskPo
    ) {

        return alarmService.completeTask(currentLoginUser, completeTaskPo);
    }


    @ApiOperation(value = "关闭报警", notes = "nejery")
    @RequestMapping(value = "/closeTask", method = RequestMethod.POST)
    @ResponseBody
    public Result closeTask(
            CurrentLoginUser currentLoginUser,
            @RequestBody CloseTaskPo closeTaskPo
    ) {

        return alarmService.closeTask(currentLoginUser, closeTaskPo);
    }

    /**
     * 保安查明情况 - 结束流程
     *
     * @param json
     * @return
     */
    @ApiOperation(value = "保安做记录 - 报警处理完毕", notes = "daibin")
    @ApiImplicitParam(name = "json", value = "类似于这样传 -> {taskId:'任务id',name:'处理人名字',alarmRecord:'报警记录'}", required = true)
    @RequestMapping(value = "/overSos", method = RequestMethod.POST)
    @ResponseBody
    public Result overSos(
            CurrentLoginUser currentLoginUser,
            @RequestBody Map json
    ) {
        // return sosService.overSos(currentLoginUser,json);
        return new Result();
    }

    /**
     * 班主任处理sos报警
     *
     * @param json
     * @return
     */
    @ApiOperation(value = "班主任处理学生报警", notes = "daibin")
    @ApiImplicitParam(name = "json", value = "类似于这样传 -> {taskId:'任务id',name:'班主任名字',teacherRecord:'班主任处理记录',isGoOn:'yes/no'}", required = true)
    @RequestMapping(value = "/teacherHandle", method = RequestMethod.POST)
    @ResponseBody
    public Result teacherHandle(
            CurrentLoginUser currentLoginUser,
            @RequestBody Map json
    ) {
        // return sosService.teacherHandle(currentLoginUser,json);
        return new Result();
    }

    /**
     * 电子围栏报警
     *
     * @param json
     * @return
     */
    @ApiIgnore
    @RequestMapping(value = "/fenceSos", method = RequestMethod.POST)
    @ResponseBody
    public Result fenceSos(@RequestBody String json) {
        // return sosService.fenceSos(json);
        return new Result();
    }

    /**
     * 新增关闭报警类型选择
     *
     * @return
     */
    @ApiOperation(value = "新增关闭报警类型选择", notes = "daibin")
    @RequestMapping(value = "/addAlarmCloseType", method = RequestMethod.POST)
    @ResponseBody
    public Result addAlarmCloseType(@RequestBody AlarmCloseDesEntity alarmCloseDesEntity) {
        Result result = new Result();
        result.setData(alarmCloseDesDao.insert(alarmCloseDesEntity));
        return result;
    }

    /**
     * 删除关闭报警类型选择
     *
     * @return
     */
    @ApiOperation(value = "删除关闭报警类型选择", notes = "daibin")
    @RequestMapping(value = "/delAlarmCloseType", method = RequestMethod.DELETE)
    @ResponseBody
    public Result delAlarmCloseType(@RequestParam(value = "id") Integer id) {
        Result result = new Result();
        if (1 != alarmCloseDesDao.deleteById(id)) {
            result.setMessage("删除失败");
        }
        return result;
    }

    /**
     * 获取关闭报警类型选择
     *
     * @return
     */
    @ApiOperation(value = "关闭报警类型选择", notes = "daibin")
    @RequestMapping(value = "/chooseAlarmCloseType", method = RequestMethod.GET)
    @ResponseBody
    public Result chooseCloseType() {
        Result result = new Result();
        result.setData(alarmCloseDesDao.selectList(new QueryWrapper<AlarmCloseDesEntity>()));
        return result;
    }


}
